using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Threading;

namespace MSNClient
{
    public partial class MessageForm : Form
    {
        private Font contactNameFont = new Font(FontFamily.GenericSansSerif, 10f, FontStyle.Bold);
        public bool IsClosed;
        private string iNickName = ""; //Stores user nickname for current message window
        public MessageForm()
        {
            this.MinimumSize = new Size(200, 200);
            Color iBackColor = Color.White;
            InitializeComponent();
            
            iClientText.KeyDown += new KeyEventHandler(iClientText_KeyDown);

            iChatData.KeyPress += new KeyPressEventHandler(iChatData_KeyPress);
            iChatData.KeyDown += new KeyEventHandler(iChatData_KeyDown);
        }

        public void ClearUserEneterdText()
        {
            this.iClientText.Clear();
        }

        void iChatData_KeyDown(object sender, KeyEventArgs e)
        {

            if (e.Control && e.KeyCode == Keys.C)
            {
            }
            else
            {
                e.Handled = true;
            }

        }

        void iChatData_KeyPress(object sender, KeyPressEventArgs e)
        {
            e.Handled = true;
        }

        void iClientText_KeyDown(object sender, KeyEventArgs e)
        {
            if (!e.Control && e.KeyCode == Keys.Enter)
            {
                buttonSent.PerformClick();
                e.Handled = true;
            }
        }

        public void RegisterNudgeButtonClickEvent(EventHandler eventHandler)
        {
            this.buttonNudge.Click += eventHandler;
        }

        public void RegisterSentButtonClickEvent(EventHandler eventHandler)
        {
            this.buttonSent.Click += eventHandler;
        }



        public String AccountID
        {
            get { return this.Name; }
            set { this.Name = value; }
        }
        public string AppendToChatHistory
        {
            set { iChatData.AppendText(value); }
        }

        public string GetUserEnteredText
        {
            get { return iClientText.Text; }
        }

        public void SetText(String text)
        {
            iChatData.AppendText(text);
        }
        protected override void OnLoad(EventArgs e)
        {
            IsClosed = false;
            base.OnLoad(e);
        }
        protected override void OnClosed(EventArgs e)
        {
            IsClosed = true;
            base.OnClosed(e);
        }
        public void AddMessage(MessageData aMessage)
        {
            iChatData.Select(iChatData.TextLength, 0);
            iChatData.ScrollToCaret();

            iChatData.SelectionColor = Color.Red;
            iChatData.SelectionFont = contactNameFont;
            iChatData.SelectedText = GetTimeStamp() + aMessage.sender + ": ";

            iChatData.SelectionColor = aMessage.textColor;
            iChatData.SelectionFont = new Font(FontFamily.GenericSansSerif, 10f, aMessage.GetFontStyle);
            iChatData.SelectedText = aMessage.text + "\n";
            iChatData.Select(iChatData.TextLength, 0);
            iChatData.ScrollToCaret();
        }
        public void AddMessage(String aMessage, String aAccountID)
        {
            iChatData.Select(iChatData.TextLength, 0);
            iChatData.ScrollToCaret();

            iChatData.SelectionColor = Color.Blue;
            iChatData.SelectionFont = contactNameFont;
            iChatData.SelectedText = GetTimeStamp() + aAccountID + ": ";

            iChatData.SelectionColor = Color.Black;
            iChatData.SelectionFont = new Font(FontFamily.GenericSansSerif, 10f, FontStyle.Bold);
            iChatData.SelectedText = aMessage + "\n";
            iChatData.Select(iChatData.TextLength, 0);
            iChatData.ScrollToCaret();
        }
        private String GetTimeStamp()
        {
            DateTime timeNow = DateTime.Now;
            return String.Format("[{0:00}:{1:00}:{2:00}] ", timeNow.Hour, timeNow.Minute, timeNow.Second);
        }
    }
}